<template>
  <div class="app-container">
    <div>
      <el-button @click="handleAdd" class="category">分类添加</el-button>
      <el-button @click="handleImport" class="Categories">导入分类</el-button>
    </div>
    <!-- <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px" size="medium"
      class="ry_form">
      <el-form-item label="分类编码" prop="number">
        <el-input v-model.trim="queryParams.number" placeholder="请输入分类编码" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分类名称" prop="name">
        <el-input v-model.trim="queryParams.name" placeholder="请输入分类名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="父节点" prop="parentId">
        <el-input v-model.trim="parentName" disabled size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="显示排序" prop="orderNo">
        <el-input v-model.trim="queryParams.orderNo" placeholder="请输入显示排序" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item class="flex_one tr">
        <el-button size="mini" @click="handleQuery">搜索</el-button>
        <el-button size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button plain size="mini" @click="handleAdd" v-hasPermi="['wms:AssetCategory:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['wms:AssetCategory:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['wms:AssetCategory:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" :loading="exportLoading"
          @click="handleExport" v-hasPermi="['wms:AssetCategory:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="refresh" :columns="columns"></right-toolbar>
    </el-row>

    <vxe-table v-loading="tableData.loading" :data="tableData.list">
      <vxe-column type="checkbox" width="55" align="center"></vxe-column>
      <vxe-column field="number" title="分类编码" align="center"></vxe-column>
      <vxe-column field="name" title="分类名称" align="center"></vxe-column>
      <vxe-column field="parentId" title="父节点ID" align="center"></vxe-column>
      <vxe-column field="orderNo" title="显示排序" align="center"></vxe-column>
      <vxe-column field="remark" title="备注" align="center"></vxe-column>
      <vxe-column title="状态" align="center">
        <template #default="{ row }">
          <el-switch v-model="row.status" :active-value="1" :inactive-value="0" @change="handleStatusChange(row)"
            active-color="#13ce66" inactive-color="#ff4949">
          </el-switch>
        </template>
      </vxe-column>
      <vxe-column title="操作" align="center" width="180">
        <template #default="{ row }">
          <el-button size="small" link type="primary" @click="handleUpdate(row)">修改</el-button>
          <el-button size="small" link type="primary" @click="handleDelete(row)">删除</el-button>

        </template>
      </vxe-column>
    </vxe-table>
    <div class="pagination-container">
      <pagination v-show="tableData.paging.total > 0" :total="tableData.paging.total"
        :page.sync="tableData.paging.pageNum" :limit.sync="tableData.paging.pageSize" @pagination="getList" />
    </div>
    <Edit ref="editDialog" @sub="getList"></Edit>
  </div>
</template>
<script setup>
import { listAssetCategory, delAssetCategory, exportAssetCategory, } from "@/api/asset/assetCategory.js";

import Edit from "./edit.vue";
import { ElMessageBox, ElMessage } from 'element-plus';

const editDialog = ref();
const confirm = true;
const tableData = reactive({
  loading: false,
  list: [],
  paging: {
    pageNum: 1,
    pageSize: 10,
    total: 0,
  }
})

const parentId = ref(null);

function getParams() {
  const params = {};
  Object.assign(params, tableData.paging);
  params.parentId = parentId.value;
  return params;
}

async function getList() {
  tableData.loading = true;
  try {
    const response = await listAssetCategory(getParams());
    const { content, totalElements } = response;
    tableData.list = content;
    tableData.paging.total = totalElements;
    tableData.loading = false;
  }catch(err) {
    tableData.loading = false;
    console.log(err);

  }

} async function handleStatusChange(row) {
 
}
function handleAdd() {
  editDialog.value.open();
}

function handleUpdate(row) {
  editDialog.value.open(row);
}

async function refresh() {
  tableData.paging.pageNum = 1;
}

function handleDelete(row) {
  const ids = row ? [row.id] : this.ids;
  if (ids.length > 0) {
    ElMessageBox.confirm("是否删除选中分类?", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
    }).then(() => {
      delAssetCategory(ids).then(() => {
        ElMessage.success("删除成功");
        getList();
      }).catch(() => {
        ElMessage.error("删除失败");
      });
    });
  } else {
    this.$message.warning("请选择要删除的分类");
  }
}



onMounted(()=> {

  getList();
})

</script>

<style lang="scss" scoped>
.app-container {
  display: flex;
  flex-direction: column;
  height: 92vh;
  overflow: hidden;
}
.category{
  width: 73.6px;
  height: 31.6px;
  background-color: #005bf5;
  color: #fff;
  border: 1px solid #fff;
  border-radius: 6%;
}
.vxe-table {
  flex: 1;
  overflow: auto;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
}
.Categories{
    width: 73.6px;
    height: 31.6px;
    color:black;
    border-radius: 6%;
}
.mb8{
margin-bottom:5px ;
}
</style>
